import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/mixin/base_page.dart';

import '../widgets/app_widgets.dart';

///带标题的页面
abstract class PageWithTitle extends StatefulWidget with BasePage {
  ///页面标题
  final String title;

  ///带标题的页面
  ///- [title] 页面标题
  ///- [key] 组件id，这里直接为null，表示当框架检测到widget更新，同类型的Widget会直接复用已
  ///经创建的Element
  const PageWithTitle(this.title, {super.key});

  ///构建内容组件列表
  /// - [context] 当前组件上下文
  /// - return 组件列表，建议使用以下组件快速构建页面
  ///   - PrimaryTitle 分段内容标题
  ///   - AppSpacer.large 组件间隔
  ///   - UlItem 无序列表项
  ///   - OlItem 有序列表项
  ///   - BodyText 主要文本
  ///   - LabelText 描述文本
  List<Widget> buildContentWidgets(BuildContext context);

  @override
  State<StatefulWidget> createState() {
    return PageWithTitleState();
  }
}

class PageWithTitleState extends State<PageWithTitle> {
  ScrollPhysics _physics = const AlwaysScrollableScrollPhysics();

  set physics(ScrollPhysics scrollPhysics) => setState(
        () {
          print("set physics");
          if (_physics != scrollPhysics) {
            setState(() {
              _physics = scrollPhysics;
            });
          }
        },
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Container(
          width: 200,
          height: 40,
          alignment: Alignment.center,
          child: AutoSizeText(
            widget.title,
            maxLines: 2,
          ),
        ),
        centerTitle: true,
      ),
      body: PagePadding(
        ListView(
          physics: _physics,
          children: widget.buildContentWidgets(context) +
              [
                AppSpacer.large,
              ],
        ),
      ),
    );
  }
}
